<?php

namespace app\api\controller\test;

use app\admin\model\Cmdlog;
use app\admin\model\Linedevice;
use app\admin\model\Roomclean;
use app\admin\model\Shop;
use app\admin\model\Shopcharge;
use app\admin\model\Shopchargeconfig;
use app\admin\model\Shopdaytrade;
use app\admin\model\Shopdevice;
use app\admin\model\Shoporder;
use app\admin\model\Shoporderprofit;
use app\admin\model\Shoproombook;
use app\admin\model\Shoptableorder;
use app\admin\model\Tuanorder;
use app\admin\model\Userbalance;
use app\admin\model\Userorderprofit;
use app\admin\model\Usertuan;
use app\api\service\DeviceService;
use app\api\service\RoomService;
use app\common\controller\Api;


class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    /**
     * 店铺汇总
     */
    public function sum()
    {

        $page = $this->request->param('page');
        $limit = 30;
        $offset = ($page - 1) * $limit;

        $shopModel = new Shop();
        $shopOrderModel = new Shoporder();
        $chargeModel = new Shopcharge();
        $tuanOrderModel = new Tuanorder();
        $list = $shopModel->limit($offset, $limit)->select();

        $shopTradeModel = new Shopdaytrade();

        $tqTableOrderModel = new Shoptableorder();
        foreach ($list as $key => $val) {

            $book_amount = $shopOrderModel->where('shop_id', $val['id'])->whereTime('create_time', 'yesterday')->where('status', 'not in', ['paying', 'refund'])->sum('amount');
            $book_pay_amount = $shopOrderModel->where('shop_id', $val['id'])->whereTime('create_time', 'yesterday')->where('status', 'not in', ['paying', 'refund'])->sum('pay_amount');
            $charge_amount = $chargeModel->where('shop_id', $val['id'])->whereTime('create_time', 'yesterday')->where('status', 'payed')->sum('amount');
            $tuan_amount = $tuanOrderModel->where('shop_id', $val['id'])->whereTime('create_time', 'yesterday')->where('status', 'payed')->sum('amount');

            $tq_amount = $tqTableOrderModel->whereTime("book_end", 'yesterday')->where('shop_id', $val['id'])->where('status', 'done')->sum('amount');


            $balance_amount = $shopOrderModel->where('shop_id', $val['id'])->whereTime('create_time', 'yesterday')->where('status', 'not in', ['paying', 'refund'])->sum('balance');
            $balance_amount2 = $tqTableOrderModel->where('shop_id', $val['id'])->whereTime('create_time', 'yesterday')->where('status', 'not in', ['paying', 'refund'])->sum('balance');

            $refund_amount = $shopOrderModel->where('shop_id', $val['id'])->whereTime('create_time', 'yesterday')->where('status', 'refund')->sum('pay_amount');


            $mtCheckTotalAmount = $shopOrderModel->whereTime("create_time", 'yesterday')->where('mt_code', "<>", "")->where('shop_id', $val['id'])->sum('amount');
            $mtCheckPayAmount = $shopOrderModel->whereTime("create_time", 'yesterday')->where('mt_code', "<>", "")->where('shop_id', $val['id'])->sum('pay_amount');

            $douCheckTotalAmount = $shopOrderModel->whereTime("create_time", 'yesterday')->where('dou_code', "<>", "")->where('shop_id', $val['id'])->sum('amount');
            $douCheckPayAmount = $shopOrderModel->whereTime("create_time", 'yesterday')->where('dou_code', "<>", "")->where('shop_id', $val['id'])->sum('pay_amount');


            $mtCheckTotalAmount2 = $tqTableOrderModel->whereTime("book_end", 'yesterday')->where('mt_code', "<>", "")->where('shop_id', $val['id'])->sum('amount');
            $douCheckTotalAmount2 = $tqTableOrderModel->whereTime("book_end", 'yesterday')->where('dou_code', "<>", "")->where('shop_id', $val['id'])->sum('amount');


            $order_tuan = $shopOrderModel->whereTime("create_time", 'yesterday')->where('tuan_id', "<>", "-1")->where('shop_id', $val['id'])->sum('amount');

            $tq_profit = $tqTableOrderModel->whereTime("book_end", 'yesterday')->where('shop_id', $val['id'])->where('status', 'done')->sum('pay_amount');

            $mtAmount = $mtCheckTotalAmount - $mtCheckPayAmount + $mtCheckTotalAmount2;
            $douAmount = $douCheckTotalAmount - $douCheckPayAmount + $douCheckTotalAmount2;

            $profit = $book_pay_amount + $charge_amount + $tuan_amount + $tq_profit;
            $profit = $profit * $val['ratio'] / 100;

            $db = array();
            $db['shop_id'] = $val['id'];
            $db['org_id'] = $val['org_id'];
            $db['shop_name'] = $val['name'];
            $db['create_time'] = strtotime("-1 day", time());
            $db['book_amount'] = $book_amount;
            $db['book_pay_amount'] = $book_pay_amount;
            $db['charge_amount'] = $charge_amount;
            $db['tuan_amount'] = $tuan_amount;
            $db['dou_amount'] = $douAmount;
            $db['mt_amount'] = $mtAmount;
            $db['profit'] = $profit;
            $db['refund_amount'] = $refund_amount;
            $db['balance_amount'] = $balance_amount + $balance_amount2;
            $db['tq_amount'] = $tq_amount;
            $db['total_amount'] = $tq_amount + $book_amount;
            $db['order_tuan'] = $order_tuan;
            $db['tq_pay_amount'] = $tq_profit;

            $totalAmount = $tq_amount + $book_amount;

            if ($totalAmount > 0) {
                $shopTradeModel->insert($db);
            }
        }
        $this->success("成功");

    }


    public function deviceList()
    {

        $keyword = $this->request->param('keyword');
        $where = array();
        if ($keyword != "") {
            $where['device_sn'] = ['like', '%' . $keyword . '%'];

        }
        $deviceModel = new Shopdevice();
        $list = $deviceModel->where($where)->where('is_test', -1)->select();
        $this->success('', $list);
    }

    public function done()
    {
        $device_no = $this->request->param('device_no');
        $deviceModel = new Shopdevice();
        $d = $deviceModel->where('device_sn', $device_no)->find();
        $deviceModel->where('id', $d['id'])->update(['is_test' => 1]);

        $this->success('');
    }

    public function cmdTest()
    {

        $device_no = $this->request->param('device_no');
        $shopDeviceModel = new Shopdevice();
        $device = $shopDeviceModel->where('device_sn', $device_no)->find();
        $order_no = getOrderSn();
        $db = array();
        $db['order_no'] = $order_no;
        $db['shop_id'] = -10;
        $db['room_id'] = -10;
        $db['user_id'] = -10;
        $db['create_time'] = time();


        $req = array();
        $req['orderNo'] = $order_no;
        $req['sn'] = $device['sn'];
        $req['sec'] = 60;

        $db['cmd'] = json_encode($req);

        $cmdModel = new Cmdlog();
        $cmdModel->insert($db);

        $ret = http_post_json($this->openCmdUrl, json_encode($req));
        $ret = json_decode($ret, true);

        error_log("===ret==" . json_encode($ret));
        $this->success('已发送');
    }
}
